import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: [],
        admin: []
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("administer").where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let array = [];
                for (let i of res.data) {
                    array.push(i.unionId);
                }
                This.setData({ admin: array });
            });
        await db.collection("sign-in").where({
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = res.data;
                for (let i of member) {
                    console.log();
                    if (this.data.admin.indexOf(i.unionId) != -1) {
                        i.admin = true;
                    }
                }
                This.setData({ member: member });
            });
    },
    async delete(e) {
        await Dialog.confirm({
                title: '提示',
                message: '请确认该操作',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false;
                const This = this;
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    if (!e.target.dataset.admin) {
                        await db.collection("administer")
                            .add({
                                data: {
                                    unionId: e.target.dataset.id,
                                    alliance: app.data.alliance,
                                    userName: e.target.dataset.username,
                                }

                            })
                            .then(res => {
                                Toast('修改成功');
                            })
                            .catch(err => {
                                Toast('修改失败');
                            })
                    } else {
                        await db.collection("administer")
                            .where({
                                unionId: e.target.dataset.id,
                                alliance: app.data.alliance
                            }).remove()
                            .then(res => {
                                console.log(res);
                                Toast('修改成功');
                            })
                            .catch(err => {
                                Toast('修改失败');
                            })
                    }
                    await db.collection("administer").where({
                            alliance: app.data.alliance
                        })
                        .get()
                        .then(res => {
                            let array = [];
                            for (let i of res.data) {
                                array.push(i.unionId);
                            }
                            This.setData({ admin: array });
                        });
                    await db.collection("sign-in").where({
                            alliance: app.data.alliance
                        })
                        .get()
                        .then(res => {
                            let member = res.data;

                            for (let i of member) {
                                if (this.data.admin.indexOf(i.unionId) != -1) {
                                    i.admin = true;
                                }
                            }
                            This.setData({ member: member });
                        });
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },


})